//
//  TEAlertPresentationController.m
//  TuringExchange
//
//  Created by LFY on 2017/9/30.
//  Copyright © 2017年 tuling. All rights reserved.
//

#define kScreenWidth [[UIScreen mainScreen] bounds].size.width


#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#import "TEAlertPresentationController.h"

#import "TEAlertController.h"

@interface TEAlertPresentationController ()

@property (nonatomic, strong) UIButton *dismissButton;
@property (nonatomic, assign) BOOL didInitializeLayoutPresentedView;

@end

@implementation TEAlertPresentationController

- (void)containerViewWillLayoutSubviews {
    [super containerViewWillLayoutSubviews];
    if (!_didInitializeLayoutPresentedView) {
        TEAlertController *presentedController = (TEAlertController *)self.presentedViewController;
        CGSize size = presentedController.preferredContentSize;
        if (presentedController.preferredStyle == TEAlertControllerStyleAlert) {
            self.presentedView.frame = CGRectMake((kScreenWidth - size.width) / 2,
                                                  (kScreenHeight - size.height) / 2,
                                                  size.width,
                                                  size.height);
        }else {
            self.presentedView.frame = CGRectMake(0, kScreenHeight - size.height, size.width, size.height);
        }
        _didInitializeLayoutPresentedView = YES;
    }
    [self createDismissButton];
}

- (void)createDismissButton{
    if (_dismissButton) return;
    _dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.containerView insertSubview:_dismissButton atIndex:0];
    _dismissButton.frame = self.containerView.bounds;
    _dismissButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    [_dismissButton addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dismissAction{
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}

@end
